<?php
namespace App\Http\Controllers\Admin;

use App\Models\TeachesChapter;
use App\Models\TeachesContent;
use Illuminate\Http\Request;
use App\Models\TeachesContentVideo;
use App\Models\TeachesContentOffice;
use Illuminate\Support\Facades\Response;
use App\Models\TeachesContentPpt;
/**
 *
 * @author Joker
 *        
 */
class TeachesContentController extends BaseController
{
    
    
    /**
     * @see \App\Http\Controllers\Admin\BaseController::__construct()
     */
    public function __construct() {
        parent::__construct();
    }
    
    public function index($id)
    {
        if(!$this->user->can('viewTeaches')) abort(405);
        
        $chapter = TeachesChapter::find($id);
        
        $courses = TeachesContent::where('chapter_id','=',$id)->paginate(20);
        
        return view('admin.teaches.course')->with('chapter',$chapter)->with('courses',$courses);
    }
    
    public function create(Request $req)
    {
        if(!$this->user->can('editTeaches')) abort(405);
        
        $mod = $req->get('mod','step');
        $chapterid = $req->get('chapter',0);
        if($mod === 'step' || empty($mod))
        {
            $path = 'admin.teaches.step';
        }
        elseif($mod === 'video')
        {
            $path = 'admin.teaches.video';
        }
        elseif($mod === 'office')
        {
            $path = 'admin.teaches.office';
        }
        elseif($mod === 'ppt')
        {
            $path = 'admin.teaches.ppt';
        }
                
        return  view($path)->with('chapterid',$chapterid);
    }
    
    public function edit($id)
    {
        if(!$this->user->can('viewTeaches')) abort(405);
        
        
        $result = TeachesContent::find($id);
        if(empty($result))
        {
            abort(404);
        }
        $chapterid = $result->chapter_id;
        switch ($result->teaches_type)
        {
            case 1:
                $video = $result->video;
        
                return  view('admin.teaches.video')
                        ->with('result',$result)
                        ->with('chapterid',$chapterid)
                        ->with('videos',$video);
            case 2:
                $office = $result->office;
        
                return  view('admin.teaches.office')
                        ->with('result',$result)
                        ->with('office',$office)
                        ->with('chapterid',$chapterid);
            case 3:
                $office = $result->ppt;
            
                return  view('admin.teaches.ppt')
                        ->with('result',$result)
                        ->with('office',$office)
                        ->with('chapterid',$chapterid);
            default:
                $result->description =collect(unserialize($result->description));
                $result->content = collect(unserialize($result->content));
                $result->notice = collect(unserialize($result->notice));
                $result->tools = collect(unserialize($result->tools));
        
                return  view('admin.teaches.step')
                        ->with('chapterid',$chapterid)
                        ->with('result',$result);
        }   
    }
    
    public function store(Request $req)
    {
        if(!$this->user->can('editTeaches')) 
            return $this->formatJson('error',$this->permerror);
        
        $content = array();
         
        $data = $req->only('chapterid','contentid','name','description','descimg','tools','content','notice','sort','teachesType','videoid','officeid','price','need_login');
        
        if(is_numeric($data['contentid'])&&$data['contentid'] > 0)
        {
            $teaches = TeachesContent::find($data['contentid']);
            $data['teachesType'] =$teaches->teaches_type;
        }
        else
        {
            $teaches = new TeachesContent();
            $teaches->user_id = $this->user->id;
        }
        switch ($data['teachesType']){
            case 1: //视频文章
                $description = $data['description'];
                $contents = '视频教程';
                $tools = '';
                $notice = '';
                $video_id = $data['videoid'];
                break;
            case 2: //office 类系
                $description = $data['description'];
                $contents = 'office 类系教程';
                $tools = '';
                $notice = '';
                $office_id = $data['officeid'];
                break;
            case 3: //ppt教程
                $description = $data['description'];
                $contents = 'ppt 类系教程';
                $tools = '';
                $notice = '';
                $office_id = $data['officeid'];
                break;
            default:
                $description = serialize(array('name'=>$data['description'],'img'=>strToArray($data['descimg'],",")));
                $tools = serialize($data['tools']);
                $content = stepContent($data['content']);
                $contents = serialize($content);
                $notice = serialize($data['notice']);
        
        }

        $teaches->name = $data['name'];
        $teaches->chapter_id = $data['chapterid'];
        $teaches->description= $description;
        $teaches->notice = $notice;
        $teaches->tools = $tools;
        $teaches->content= $contents;
        $teaches->sort = $data['sort'];
        $teaches->teaches_type = $data['teachesType'];
        $teaches->price = $data['price'];
        $teaches->need_login = $data['need_login'];
        $teaches->save();
        
        //建立关联 文章与标签
        switch ($data['teachesType'])
        {
            case 1:
                if(count($teaches->video)>0)
                {
                    $oldVideoId = $teaches->video->id;
                    if($oldVideoId != $video_id)
                    {
                        $video = TeachesContentVideo::find($video_id);
                        $video->content_id = $teaches->id;
                        TeachesContentVideo::where("id",'=',$oldVideoId)->delete();
                        $video->save();
                    }
                }
                else 
                {
                    $video = TeachesContentVideo::find($video_id);
                    $video->content_id = $teaches->id;
                    $video->save();
                }
                break;
            case 2:
                if(count($teaches->office)>0)
                {
                    $oldOfficeId = $teaches->office->id;
                    if($oldOfficeId != $office_id)
                    {
                        $ppt=TeachesContentOffice::find($office_id);
                        $ppt->content_id = $teaches->id;
                        TeachesContentOffice::where("id",'=',$oldOfficeId)->delete();
                        $ppt->save();
                    }
                }
                else
                {
                    $ppt=TeachesContentOffice::find($office_id);
                    $ppt->content_id = $teaches->id;
                    $ppt->save();
                }
                break;
            case 3:
                if (count($teaches->ppt))
                {
                    $oldPptId = $teaches->ppt->id;
                    if($oldPptId != $office_id)
                    {
                        $ppt = TeachesContentPpt::find($office_id);
                        $ppt->content_id = $teaches->id;
                        TeachesContentPpt::where('id','=',$oldPptId)->delete();
                        $ppt->save();
                    }
                }
                else 
                {
                    $ppt = TeachesContentPpt::find($office_id);
                    $ppt->content_id = $teaches->id;
                    $ppt->save();
                }
                break;
        }
        return Response::json(array('id'=>$teaches->id,'status'=>'success','msg'=>'保存数据成功！','redirectUrl'=>'/admin/teaches/index/course/index/'.$data['chapterid']));
    }
    
    /**
     * 删除
     * @param integer $id
     */
    public function destroy($id) 
    {
        if(!$this->user->can('deleteTeaches'))
            return $this->formatJson('error',$this->permerror);
        
        $content = TeachesContent::find($id);
        if(empty($content))
            return $this->formatJson('error',$this->noResource);
        if($content->delete())
        {
            return $this->formatJson('success','删除［'.$content->name.']成功！');
        }
    }
}